using System;
using System.Collections.Generic;
using System.Text;

namespace Algorithms.Numerical.Impl
{
    public class SubmatrixViewDecorator : Matrix
    {
        private Matrix source;
        private int y;
        private int x;
        private int height;
        private int width;

        public SubmatrixViewDecorator(Matrix source, int y, int x, int height, int width)
        {
            if (y < 0 || x < 0 || height < 0 || width < 0 || (y + height) > source.Height || (x + width) > source.Width)
                throw new ArgumentException("Invalid submatrix specification");
            this.source = source;
            this.y = y;
            this.x = x;
            this.height = height;
            this.width = width;
        }

        public override int Height
        {
            get
            {
                return this.height;
            }
        }

        public override int Width
        {
            get
            {
                return this.width;
            }
        }

        private bool isInside(int y, int x)
        {
            return y >= 0 && x >= 0 && y < height && x < width;
        }

        public override double this[int y, int x]
        {
            get
            {
                if (!this.isInside(y, x))
                    throw new ArgumentException();
                return this.source[this.y + y, this.x + x];
            }
            set
            {
                if (!this.isInside(y, x))
                    throw new ArgumentException();
                this.source[this.y + y, this.x + x] = value;
            }
        }

    }
}
